using Godot;
using System;

public partial class Player : CharacterBody2D
{

	private int MAX_SPEED = 125;
	private const int ACCELERATION_SMOOTHING = 25;

	Timer damageIntervalTimer ;
	public HealthComponent healthComponent;

	VelocityComponent velocityComponent;

	ProgressBar healthBar;

	int numberCollidingBodies = 0; //怪物接触数量

	Node abilities;
	GameEvents gameEvents;//自动加载文件
	AnimationPlayer animationPlayer;

	Node2D visuals;

	int baseSpeed = 0;

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		damageIntervalTimer = GetNode<Timer>("DamageIntervalTimer");
		healthComponent = GetNode<HealthComponent>("HealthComponent");
		healthBar = GetNode<ProgressBar>("HealthBar");
		abilities = GetNode<Node>("Abilities");
		gameEvents = GetNode<GameEvents>("/root/GameEvents");
		animationPlayer = GetNode<AnimationPlayer>("AnimationPlayer");
		visuals = GetNode<Node2D>("Visuals");
		velocityComponent = GetNode<VelocityComponent>("VelocityComponent");

		baseSpeed = velocityComponent.maxSpeed;

		GetNode<Area2D>("CollisionArea2D").BodyEntered += OnBodyEntered;
		GetNode<Area2D>("CollisionArea2D").BodyExited += OnBodyExited;
		healthComponent.HealthChanged += OnHealthChanged;
		damageIntervalTimer.Timeout += OnDamageIntervalTimerTimeout;

		gameEvents.AbilityUpgradeAdded += OnAbilityUpgradeAdded;
		this.UpdateHealthDisplay();
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		Vector2 movementVector = this.GetMovementVector();//获取按键
		// GD.Print("初始",movementVector);
		Vector2 direction = movementVector.Normalized();//获取向量  
		// GD.Print("Normalized",direction);
		velocityComponent.AccelerateInDirection(direction);
		velocityComponent.Move(this);

		if (movementVector.X !=0 || movementVector.Y != 0){
			animationPlayer.Play("walk");
		}else{
			animationPlayer.Play("RESET");
		}

		int moveSign = Math.Sign(movementVector.X);
		if(moveSign != 0){
			visuals.Scale = new Vector2(moveSign,1);
		}


	}

	public Vector2 GetMovementVector(){
		//获取按键按下的情况， 对于键盘来说 获取到的值为  0或1
		var x_movement = Input.GetActionStrength("move_right") - Input.GetActionStrength("move_left"); 
		var y_movement = Input.GetActionStrength("move_down") - Input.GetActionStrength("move_up");

		return new Vector2(x_movement,y_movement);
	}

	//检测是否受到伤害
	public void CheckDealDamage(){
		if( numberCollidingBodies == 0 || !damageIntervalTimer.IsStopped()){
			return;
		}
		healthComponent.Damage(1);
		damageIntervalTimer.Start();
		GD.Print(healthComponent.currentHealth);
	}

	//血量变化
	public void UpdateHealthDisplay(){
		healthBar.Value = healthComponent.GetHealthPercent();
	}

	public void OnBodyEntered(Node2D otherBody){
		numberCollidingBodies++;
		this.CheckDealDamage();
	}

	public void OnBodyExited(Node2D otherBody){
		numberCollidingBodies--;
	}

	public void OnDamageIntervalTimerTimeout(){
		this.CheckDealDamage();
	}

	//血量变化
	public void OnHealthChanged(){
		gameEvents.EmitPlayerDamaged();
		this.UpdateHealthDisplay();

		GetNode<RandomStreamPlayer2dComponent>("HitRandomStreamPlayer").PlayRandom();
	}

	public void OnAbilityUpgradeAdded(AbilityUpgrade abilityUpgrade,int quantity){
		if(abilityUpgrade is Ability){ //旋转斧头的能力
			Ability ability = abilityUpgrade as Ability;
			abilities.AddChild(ability.abilityControllerScene.Instantiate());
		}else if(abilityUpgrade.id == "player_speed"){ //移速
			velocityComponent.maxSpeed = Convert.ToInt32(baseSpeed * (1 + quantity* 0.1));
		}
		
	}

	public override void _ExitTree(){
		//gameEvents 为自动加载类，不会销毁，所以绑定在gameEvents上的事件需要再节点退出的时候也要解除事件
		gameEvents.AbilityUpgradeAdded -= OnAbilityUpgradeAdded;
		base._ExitTree();
	}
}
